/**
 * 
 */
package com.nd.android.payment.ui.dialog;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import antistatic.spinnerwheel.OnWheelScrollListener;
import antistatic.spinnerwheel.WheelVerticalView;

import com.common.StarAppConst;
import com.common.utils.StarAppUtils;
import com.nd.android.payment.R;
import com.nd.android.payment.ui.adapter.CommonPayWheelAdapter;
import com.nd.android.payment.ui.param.CommonRecharge;
import com.nd.android.payment.ui.param.ICommonRecharge;

/**
 * 充值组件
 * 
 * <br>Created 2014-11-3 下午5:19:20
 * @version  
 * @author   张清荣		
 *
 * @see
 */

public class CommonRechargeDlg extends Dialog {
    
    /** 默认5，从0开始 */
    public final static int DEFAULT_ITEM_INDEX = 0;
    
    /** 滚动框，默认可见的个数 */
    public final int DEFAULT_VISIBLE_ITEM_NUMBER = 3;
    
    /** 上下文 */
    private Activity mActivity;

    /** 标题TextView */
    private TextView mTvTitle;
    
    /**
     * 标题TextView - 剩余的提示
     */
    private TextView mTvTitleRest;
    
    /**
     * 标题TextView - 兑换率的提示
     */
    private TextView mTvTitleRate;
    
    /** 滚动框 */
    private WheelVerticalView mWvBody;
    
    /** 余额提示 */
    private TextView mTvRest;
    /** 兑换率提示 */
    private TextView mTvRate;
    /** 提示 */
    private TextView mTvTip;
    
    /** 标题的layout */
    private LinearLayout mLlPayRechargeTitle;
    
    /** 提示的layout */
    private LinearLayout mLlPayRechargeTip;
    
    /** 左边按钮 */
    private Button mBtnLeft;

    /** 右边按钮 */
    private Button mBtnRight;
    
    /** 按钮点击监听 */
    private OnCommonRechargeClickListener mListener;
    
    /** 滚动框滚动监听 */
    private OnWheelScrollListener mWheelScrollListener;
    
    /** 参数bean */
    private ICommonRecharge mParamBean;
    
    /**
     * 按钮点击监听接口
     * 
     * <br>
     * Created 2014-10-30 下午2:40:51
     * 
     * @version CommonRechargeDlg
     * @author zhangqr
     * 
     * @see
     */
    public interface OnCommonRechargeClickListener {
        /**
         * 左按钮点击处理
         * 
         * <br>
         * Created 2014-6-9 下午5:44:48
         * 
         * @author : zhangqr
         */
        void onLeftBtnClicked();

        /**
         * 右按钮点击处理
         * 
         * @param count 滚动框中的值
         * 
         * <br>
         * Created 2014-6-9 下午5:45:16
         * 
         * @author : zhangqr
         */
        void onRightBtnClicked(int count);
    }
    
    /**
     * 创建实例
     * 
     * @param activity
     *      上下文
     * @param theme
     *      对话框样式ID
     * @param param
     *      参数bean
     */
    public CommonRechargeDlg(Activity activity, ICommonRecharge param) {
        super(activity, R.style.common_dialog_style);
        
        this.mActivity = activity;
        
        if(param == null) {
            this.mParamBean = new CommonRecharge();
        } else {
            this.mParamBean = param;
        }
        
        setCanceledOnTouchOutside(false);
        
    }

    /**
     * 创建实例
     * 
     * @param activity
     *      上下文
     * @param theme
     *      对话框样式ID
     * @param param
     *      参数bean
     */
    public CommonRechargeDlg(Activity activity, int theme, 
            ICommonRecharge param) {
        super(activity, theme);
        
        this.mActivity = activity;
        
        if(param == null) {
            this.mParamBean = new CommonRecharge();
        } else {
            this.mParamBean = param;
        }
        
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        if(mParamBean.getmLayoutResID() != 0) {//布局
            setContentView(mParamBean.getmLayoutResID());
        } else {
            setContentView(R.layout.common_pay_recharge);
        }
        
        initComponent();
        initComponentValue();
        initEvent();
        
    }
    
    /***
     * 初始化视图
     * 
     * <br>Created 2014-11-3 下午8:48:45
     * @author       zhangqr
     */
    public void initComponent() {
        
        // 设置对话框横向充满，垂直自适应并居中
        Window mWindow = this.getWindow();
        mWindow.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        mWindow.setGravity(Gravity.CENTER);
        
        mTvTitle = (TextView) findViewById(R.id.tv_pay_recharge_title);
        mTvTitleRest = (TextView) findViewById(R.id.tv_pay_recharge_title_rest);
        mTvTitleRate = (TextView) findViewById(R.id.tv_pay_recharge_title_rate);
        mTvRest = (TextView) findViewById(R.id.tv_pay_recharge_rest);
        mTvRate = (TextView) findViewById(R.id.tv_pay_recharge_rate);
        mTvTip = (TextView) findViewById(R.id.tv_pay_recharge_tip);
        
        mLlPayRechargeTitle = (LinearLayout) findViewById(R.id.ll_pay_recharge_title);
        mLlPayRechargeTip = (LinearLayout) findViewById(R.id.ll_pay_recharge_tip);
        
        mWvBody = (WheelVerticalView) findViewById(R.id.wv_pay_recharge);
        
        mBtnLeft = (Button) findViewById(R.id.btn_pay_recharge_left);
        mBtnRight = (Button) findViewById(R.id.btn_pay_recharge_right);
        
    }
    
    /***
     * 初始化视图
     * 
     * <br>Created 2014-11-3 下午8:48:45
     * @author       zhangqr
     */
    public void initComponentValue() {
        if(! TextUtils.isEmpty(mParamBean.getmStrTitle())) {//标题
            mTvTitle.setText(mParamBean.getmStrTitle());
        }
        
        if(! TextUtils.isEmpty(mParamBean.getmStrTitleRest())) {//标题 - 剩余的提示
            mTvTitleRest.setText(mParamBean.getmStrTitleRest());
        }
        
        if(! TextUtils.isEmpty(mParamBean.getmStrTitleRate())) {//标题 - 兑换率的提示
            mTvTitleRate.setText(mParamBean.getmStrTitleRate());
        }
        
        if(TextUtils.isEmpty(mParamBean.getmStrTitle())
                && TextUtils.isEmpty(mParamBean.getmStrTitleRest())
                && TextUtils.isEmpty(mParamBean.getmStrTitleRate())) {
            mLlPayRechargeTitle.setVisibility(View.GONE);
        }
        
        if(! TextUtils.isEmpty(mParamBean.getmStrRestTip())) {//余额提示
            mTvRest.setText(mParamBean.getmStrRestTip());
        } else {
            mTvRest.setVisibility(View.GONE);
        }
        
        if(! TextUtils.isEmpty(mParamBean.getmStrRateTip())) {//兑换率提示
            mTvRate.setText(mParamBean.getmStrRateTip());
        } else {
            mTvRate.setVisibility(View.GONE);
        }
        
        if(! TextUtils.isEmpty(mParamBean.getmStrTip())) {//提示
            mTvTip.setText(mParamBean.getmStrTip());
        } else {
            mTvTip.setVisibility(View.GONE);
        }
        
        if(TextUtils.isEmpty(mParamBean.getmStrRestTip())
                && TextUtils.isEmpty(mParamBean.getmStrRateTip())
                && TextUtils.isEmpty(mParamBean.getmStrTip())) {
            mLlPayRechargeTip.setVisibility(View.GONE);
        }
        
        if(! mParamBean.ismIsShowWheelView()) {
            mWvBody.setVisibility(View.GONE);
        }
        
//        mWvBody.setBackgroundResource(R.drawable.common_pay_wheel_bg);
        mWvBody.setVisibleItems(DEFAULT_VISIBLE_ITEM_NUMBER);
        if(mParamBean.getmDataList() == null
                || mParamBean.getmDataList().size() == 0) {//初始化
            ArrayList<Object> dataArr = new ArrayList<Object>();
            for(int i=0; i<100; i++) {
                dataArr.add(i);
            }
            mParamBean.setmDataList(dataArr);
        }
        mWvBody.setViewAdapter(new CommonPayWheelAdapter(mActivity,
                mParamBean.getmDataList()));
        
        mWvBody.setCurrentItem(mParamBean.getmIntDefaultItemIndex());
        
        if(! TextUtils.isEmpty(mParamBean.getmStrBtnLeft())) {//左按钮
            mBtnLeft.setText(mParamBean.getmStrBtnLeft());
        }
        
        if(! TextUtils.isEmpty(mParamBean.getmStrBtnRight())) {//右按钮
            mBtnRight.setText(mParamBean.getmStrBtnRight());
        }
        
        setTextTypeface();
    }
    
    /**
     * 初始化事件
     * 
     * <br>Created 2014-11-3 下午8:49:02
     * @author       zhangqr
     */
    public void initEvent() {
        mWvBody.addScrollingListener(mParamBean.getmWheelScrollListener());
        
        mBtnLeft.setOnClickListener(mClickListener);
        mBtnRight.setOnClickListener(mClickListener);
    }

    /**
     * 设置控件为兰亭序字体
     * 
     * <br>
     * Created 2014-8-13 下午7:30:25
     * 
     * @author zhangqr
     */
    private void setTextTypeface() {
        StarAppUtils.setViewTypeFace(mTvTitle, false);
        StarAppUtils.setViewTypeFace(mTvTitleRest, false);
        StarAppUtils.setViewTypeFace(mTvTitleRate, false);
        
        StarAppUtils.setViewTypeFace(mTvRest, false);
        StarAppUtils.setViewTypeFace(mTvRate, false);
        StarAppUtils.setViewTypeFace(mTvTip, false);
        
        StarAppUtils.setViewTypeFace(mBtnLeft, false);
        StarAppUtils.setViewTypeFace(mBtnRight, false);
    }
    
    /**
     * 点击事件
     */
    protected Button.OnClickListener mClickListener = new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (R.id.btn_pay_recharge_left == id) {
                if(mParamBean.getmListener() != null) {
                    mParamBean.getmListener().onLeftBtnClicked();
                }
                
                textChange();
                
                if(mParamBean.isDismissWhenDone()) {
                    dismiss();
                }
            } else if (R.id.btn_pay_recharge_right == id) {
                if(mParamBean.getmListener() != null) {
                    Object o = mParamBean.getmDataList().get(mWvBody.getCurrentItem());
                    try {
                        mParamBean.getmListener().onRightBtnClicked(Integer.parseInt(o.toString()));
                    } catch (Exception e) {
                        mParamBean.getmListener().onRightBtnClicked(0);
                    }
                }
                
                textChange();
                
                if(mParamBean.isDismissWhenDone()) {
                    dismiss();
                }
            } else {
                
            }
            
        }
    };
    
    /**
     * 改变文本
     * 
     * <br>Created 2014-11-7 下午2:41:42
     * @author       zhangqr
     */
    public void textChange() {
        setTitileText();
        setRestText();
        setRateText();
        setTipText();
        setRightBtnText();
        setLeftBtnText();
    }
    
    /**
     * 标题
     * 
     * <br>Created 2014-11-5 下午1:45:20
     * @param title
     * @author       zhangqr
     */
    public void setTitileText() {
        if(! TextUtils.isEmpty(mParamBean.getmStrTitle())) {//标题
            mTvTitle.setText(mParamBean.getmStrTitle());
        }
    }
    
    /**
     * 余额提示
     * 
     * <br>Created 2014-11-5 下午1:45:27
     * @param rest
     * @author       zhangqr
     */
    public void setRestText() {
        if(! TextUtils.isEmpty(mParamBean.getmStrRestTip())) {//余额提示
            mTvRest.setText(mParamBean.getmStrRestTip());
        }
    }
    
    /**
     * 兑换率提示
     * 
     * <br>Created 2014-11-5 下午1:45:34
     * @param rate
     * @author       zhangqr
     */
    public void setRateText() {
        if(! TextUtils.isEmpty(mParamBean.getmStrRateTip())) {//兑换率提示
            mTvRate.setText(mParamBean.getmStrRateTip());
        }
    }
    
    /**
     * 提示
     * 
     * <br>Created 2014-11-5 下午1:45:45
     * @param tip
     * @author       zhangqr
     */
    public void setTipText() {
        if(! TextUtils.isEmpty(mParamBean.getmStrTip())) {//提示
            mTvTip.setText(mParamBean.getmStrTip());
        }
    }
    
    /**
     * 左按钮
     * 
     * <br>Created 2014-11-5 下午1:45:49
     * @param rBtnText
     * @author       zhangqr
     */
    public void setRightBtnText() {
        if(! TextUtils.isEmpty(mParamBean.getmStrBtnLeft())) {//左按钮
            mBtnLeft.setText(mParamBean.getmStrBtnLeft());
        }
    }
    
    /**
     * 右按钮
     * 
     * <br>Created 2014-11-5 下午1:45:54
     * @param rBtnText
     * @author       zhangqr
     */
    public void setLeftBtnText() {
        if(! TextUtils.isEmpty(mParamBean.getmStrBtnRight())) {//右按钮
            mBtnRight.setText(mParamBean.getmStrBtnRight());
        }
    }
    
}
